﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frmdeposit : Form
    {
        int cid;
        DataSet ds;
        public frmdeposit()
        {
            InitializeComponent();
        }
        public frmdeposit(int id)
        {
            cid = id;
            InitializeComponent();
        }
        private void btnsub_Click(object sender, EventArgs e)
        {
            int row = -1;
            row = dgvbuylist.SelectedCells[0].RowIndex;
            if (row!=-1)
            {
                int pid = (int)ds.Tables[0].Rows[row]["pID"];
                string sql = "SELECT pid ,dpnum FROM depositinfo where Pid=" + pid + " and cid=" + cid;
                int m = (int)ds.Tables[0].Rows[0]["dpnum"];
                if (m > 0)
                {
                    string sql1 = string.Format("update indepositinfo set [dpnum] = {0} where [Pid] = {1}", m - 1, pid);
                    DialogResult n = MessageBox.Show("你确定将该产品减一吗？", "提示", MessageBoxButtons.YesNo);
                    if (n == DialogResult.Yes)
                    {
                        int k = DbHelperOleDb.ExecuteSql(sql1);

                        showbind();
                    }
                }
                //删除
                else
                {
                    string sql1 = "DELETE FROM  indepositinfo WHERE pID=" + pid;
                    DialogResult n = MessageBox.Show("该产品已经吃完你确定要删除吗？", "提示", MessageBoxButtons.YesNo);
                    if (n == DialogResult.Yes)
                    {

                        int i = DbHelperOleDb.ExecuteSql(sql);
                        if (i == 1)
                        {
                            MessageBox.Show("删除成功！");
                        }
                        else
                        {
                            MessageBox.Show("删除失败！");
                        }
                    }
                    showbind();
                }
            }
        }
        private void btnadd_Click(object sender, EventArgs e)
        {

            frmCustomerBuy f = new frmCustomerBuy(cid, 2);
            f.Text = DbHelperOleDb.GetSingle("select cname from customer where cid=" + cid).ToString() + "寄存产品！";
            f.ShowDialog();
            showbind();
        }

        private void frmdeposit_Load(object sender, EventArgs e)
        {
            showbind();

        }

        private void showbind()
        {
            string sql = @"SELECT ProductInfo.PName, ProductInfo.PID, indepositinfo.dpnum, indepositinfo.ddate, indepositinfo.cid FROM ProductInfo INNER JOIN indepositinfo ON ProductInfo.PID = indepositinfo.PID  where indepositinfo.cid= "+cid;
             ds = DbHelperOleDb.Query(sql);
            dgvbuylist.AutoGenerateColumns = false;
            dgvbuylist.DataSource = ds.Tables[0];
            //dgvbuylist.Columns[2].Visible = false;
            //单元格内内容比较多时，设置自动换行
            dgvbuylist.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            //设置根据内容的多少自动调整单元格的高度
            dgvbuylist.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        }

        private void dgvbuylist_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btndeplog_Click(object sender, EventArgs e)
        {
            debind();
        }

        private void debind()
        {
            string sql = @"SELECT ProductInfo.PID, ProductInfo.PName, customer.cid, depositinfo.dpnum, depositinfo.ddate FROM customer INNER JOIN (ProductInfo INNER JOIN depositinfo ON ProductInfo.PID = depositinfo.PID) ON customer.cid = depositinfo.CID  where depositinfo.CID="+cid;
            ds = DbHelperOleDb.Query(sql);

            dgvbuylist.AutoGenerateColumns = false;
            dgvbuylist.AutoGenerateColumns = false;
            dgvbuylist.DataSource = ds.Tables[0];
            //单元格内内容比较多时，设置自动换行
            dgvbuylist.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            //设置根据内容的多少自动调整单元格的高度
            dgvbuylist.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        }
        //寄存浏览
        private void button1_Click(object sender, EventArgs e)
        {
            showbind();
        }

        private void btnOUT_Click(object sender, EventArgs e)
        {
            myfun.DataToExcel(dgvbuylist);
        }

        private void btnprint_Click(object sender, EventArgs e)
        {
            myprint m = new myprint(dgvbuylist);
            m.Print();
        }


    }
}
